package wangjg.coco.common;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class GoogleTranslator {

	public String en2cn(String text){
		try {
			text = URLEncoder.encode(text,"utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String url = "http://translate.google.com.hk/translate_a/t?client=t&text="+text+"&hl=zh-CN&sl=en&tl=zh-CN&multires=1&otf=1&ssel=0&tsel=0&sc=1";
		return this.loadHTML(url);
	}
	
	public String cn2en(String text){
		try {
			text = URLEncoder.encode(text,"utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String url = "http://translate.google.com.hk/translate_a/t?client=t&text="+text+"&hl=zh-CN&sl=zh-CN&tl=en&multires=1&otf=1&pc=1&ssel=5&tsel=5&sc=1";
		return this.loadHTML(url);
	}
	
	private String loadHTML(String url){
		HttpRequest req = new HttpRequest(url);
		return req.load();
	}

	public static void main(String[] args) throws IOException {
		String s=new GoogleTranslator().en2cn("Prints a String and then terminate the line.");
		System.out.println(s);
	}
}
